<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      table {
        width: 500px;
        text-align: center;
        border-collapse: collapse;
      }
      th,
      td {
        border: 1px solid purple;
      }
    </style>
  </head>

  <body>
    <input type="text" placeholder="请输入订单状态" class="orderStatus" />
    <button>筛选订单</button><br /><br />
    <table>
      <thead>
        <tr>
          <th>订单编号</th>
          <th>姓名</th>
          <th>地址</th>
          <th>状态</th>
          <th>其他</th>
        </tr>
      </thead>
      <tbody>
        <!-- <tr>
          <td>1</td>
          <td>2</td>
          <td>3</td>
          <td>4</td>
          <td><a href="#" data-id="1">查看物流其它信息</a></td>
        </tr> -->
      </tbody>
    </table>

    <script>
      const orders = [
        {
          id: 1,
          name: 'John Smith',
          address: '123 Main St',
          status: 'created',
          log: [
            {
              status: 'created',
              time: '2023-02-16 12:30:00',
            },
          ],
        },
        {
          id: 2,
          name: 'Jane Doe',
          address: '456 Second St',
          status: 'packed',
          log: [
            {
              status: 'packed',
              time: '2023-02-16 12:30:00',
            },
            {
              status: 'created',
              time: '2023-02-16 10:00:00',
            },
          ],
        },
        {
          id: 3,
          name: 'Bob Johnson',
          address: '789 Third St',
          status: 'shipped',
          log: [
            {
              status: 'created',
              time: '2023-02-15 10:00:00',
            },
            {
              status: 'packed',
              time: '2023-02-15 12:00:00',
            },
            {
              status: 'shipped',
              time: '2023-02-16 9:00:00',
            },
          ],
        },
        {
          id: 4,
          name: 'zs',
          address: '789 Third St',
          status: 'created',
          log: [
            {
              status: 'created',
              time: '2023-04-15 10:00:00',
            },
          ],
        },
      ]
      const tbody = document.querySelector('tbody')
      const input = document.querySelector('.orderStatus')
      const btn = document.querySelector('button')

      const data = orders.filter((item) => {
        return item.log.sort(function (a, b) {
          if (a.status > b.status) {
            return 1
          } else {
            return -1
          }
        })
      })

      const render = function (arr) {
        const newArr = arr
          .map(({ id, name, address, status, log }, index) => {
            return `  <tr>
          <td>${id}</td>
          <td>${name}</td>
          <td>${address}</td>
          <td>${status}</td>
          <td><a href="#" data-id="${id}">查看物流其它信息</a></td>
        </tr>`
          })
          .join('')
        tbody.innerHTML = newArr
      }
      render(data)
      btn.addEventListener('click', function () {
        if(input.value==='') return alert('请输入内容')
        let newDate = data
        newDate = data.filter((item) => {
          return item.status === input.value
        })
        newDate = data.filter((item) => {
          return item.status === input.value
        })
        newDate = data.filter((item) => {
          return item.status === input.value
        })
        render(newDate)
      })
      tbody.addEventListener('click', function (e) {
        if (e.target.tagName === 'A') {
          let id = +e.target.dataset.id
          const newDate = data.filter((item) => item.id === id)
          newDate.forEach((item) => {
            alert(
              `物流信息为\n` +
                item.log
                  .map((item) => {
                    return `[${item.time}]-[${item.status}]`
                  })
                  .join('\n\n')
            )
          })
        }
      })
    </script>
  </body>
</html>
